//
//  PercentViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/17.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

import WebKit

import Charts

import SnapKit

class PercentViewController: BaseViewController,ChartViewDelegate {
    
    
    let wkWeb = WKWebView(frame: CGRect.zero)
    
    let _pieChartView = PieChartView.init(frame: CGRect.init(x: 0, y: 20, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.width));
    
    
    override var names: Array<String>{
        return ["datas"]
    }
    
    override func path(for name: String) -> String {
        return ContractResolveDegree
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        return [:]
    }
    
    override func success(for name: String) -> NetSuccess {
        
        func suc(json: JSON)->Void{
            
            print(json)
            
            guard let data = json.contents?["data"]?.contents else {
                self.alert("数据为空")
                return
            }
            
            guard let I2 = data["I2"]?.double else { return  }
            
            guard let GP = data["GP"]?.double else { return  }
            
            guard let P2 = data["P2"]?.double else { return  }
            
            guard let EC = data["EC"]?.double else { return  }
            
            guard let IC = data["IC"]?.double else { return  }
            
            drawPieChartView(with: I2, GPData: GP, P2: P2, EC: EC, IC: IC)
            
        }
        
        return suc
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        _pieChartView.delegate = self
        
//        ViewPortHandler.init(width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.width)
        
        _pieChartView.chartDescription?.enabled = false
        
        _pieChartView.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 30);//设置这块饼的位置
        
        _pieChartView.usePercentValuesEnabled = false;//转化为百分比
        
        _pieChartView.dragDecelerationEnabled = false;//我把拖拽效果关了
        
        _pieChartView.drawEntryLabelsEnabled = false;//显示区块文本
        
        _pieChartView.entryLabelFont = UIFont.systemFont(ofSize: 10);//区块文本的字体
        
        _pieChartView.entryLabelColor = UIColor.white;
        
        _pieChartView.drawSlicesUnderHoleEnabled = true;
        
        _pieChartView.drawHoleEnabled = false;//这个饼是空心的
        
        _pieChartView.drawEntryLabelsEnabled = false
        
        //图例样式设置
        _pieChartView.legend.maxSizePercent = 1;//图例的占比
        
        _pieChartView.legend.form = .square//图示：原、方、线
        
        _pieChartView.legend.formSize = 8;//图示大小
        
        _pieChartView.legend.formToTextSpace = 4;//文本间隔
        
        _pieChartView.legend.font = UIFont.systemFont(ofSize: 8);
        
        _pieChartView.legend.textColor = UIColor.gray;
        
        _pieChartView.legend.horizontalAlignment = .left;
        
        _pieChartView.legend.verticalAlignment = .top;
        
        _pieChartView.drawMarkers = true
        
        let markerView = YXBalloonMarker(color: UIColor.black.withAlphaComponent(0.5), font: UIFont.systemFont(ofSize: 12), textColor: .white, insets: UIEdgeInsets.zero)
        
        markerView.minimumSize = CGSize.init(width: 50, height: 30)
        
        markerView.chartView = _pieChartView
        
        _pieChartView.marker = markerView
//        barChart.marker = markerView
        
//        _pieChartView.marker
        
//        _pieChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeInBack);
        
        self.view.addSubview(_pieChartView)
        
//        drawPieChartView()
        self.load(with: "datas")
        
    }
    
    override func method(for name: String) -> NetMethod {
        return .get
    }
    
    func drawPieChartView(with I2Data:Double,GPData:Double,P2:Double,EC:Double,IC:Double) {
        let titles = ["I2事业部","GP事业部","P2事业部","EC事业部","IC事业部"];
        let yData = [I2Data,GPData,P2,EC,IC];
        var yVals = [PieChartDataEntry]();
        for i in 0...titles.count-1 {
            
            let entry = PieChartDataEntry.init(value: Double(yData[i]), label: titles[i]);
            
            yVals.append(entry);
        }
        
        let dataSet = PieChartDataSet.init(values: yVals, label: "");
        dataSet.colors = [UIColor.init(red: 29 / 255, green: 220 / 255, blue: 113 / 255, alpha: 1),UIColor.hex("8c59d3"),UIColor.hex("eedc63"),UIColor.hex("0bc6f2"),UIColor.hex("ed4a47")];
        //设置名称和数据的位置 都在内就没有折线了哦
        dataSet.xValuePosition = .insideSlice;
        dataSet.yValuePosition = .outsideSlice;
        dataSet.sliceSpace = 0
        dataSet.selectionShift = 10;//选中放大半径
        //指示折线样式
        dataSet.valueLineVariableLength = false
        dataSet.valueLinePart1OffsetPercentage = 0.8 //折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远
        dataSet.valueLinePart1Length = 0.8 //折线中第一段长度占比
        dataSet.valueLinePart2Length = 0.4 //折线中第二段长度最大占比
        dataSet.valueLineWidth = 0 //折线的粗细
        dataSet.valueLineColor = UIColor.brown //折线颜色
        
        
        let data = PieChartData.init(dataSets: [dataSet]);
        data.setValueFormatter(VDChartAxisValueFormatter.init());//格式化值（添加个%）
        data.setValueFont(UIFont.systemFont(ofSize: 10.0));
        data.setValueTextColor(UIColor.lightGray);
        _pieChartView.data = data;
        
        
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    }
    
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight)->Void{
        print(chartView)
//        print("drawAngles" + chartView.drawMarkers)
    }
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
